import {Obj} from "@/pages/wordPage/src/interface/Obj";
import screen from "@/pages/wordPage/src/screen";
import utils from "@/pages/wordPage/src/utils";
import {ElMessage} from "element-plus";
import des from './des';
import prr from 'p5';
import ctxUtils from "@/pages/wordPage/src/ctxUtils";
import message from "@/pages/wordPage/src/objs/message";
import config from "@/config";
import setting from "@/setting";
// import {CENTER} from "@/assets/p5";
// import {CENTER} from "@/assets/p5";
export default class word extends Obj{
    /**
     * 单词内容
     * 单词备注
     * @param context
     * @param des
     */
    ctx;
    des; //单词描述
    context//单词名称
    canColliding=true;
    height=setting.value.fontSize+10;
    showAfterCreateMaxTime=Date.now()+100;//创建后显示动画最大生命值
    width=80;
    zIndex=0;
    fontSize=setting.value.fontSize;
    maxTryGen=100;
    lastClick=0;
    doubleClickMaxDelay=500;
    color="rgba(8,246,204,0.47)"
    isSelected=false
    level=1;
    constructor(context,des,c,level) {
        super();
        this.name="单词"
        this.context=context;
        this.des=des;
        this.width=this.calculateTextWidth(context)+20
        this.ctx=c;
        let x;
        let y;
        let trys=0;
        this.level=level;



        while(true){
            x=Math.random()*screen.width-this.width;
            y=Math.random()*screen.height-this.height;
            trys++;
            if (trys>this.maxTryGen){
                this.destroy();
                new message("溢出！请设置最大页码单词数")

                break;
            }
            this.x=x;
            this.y=y;
            if (utils.findColliding(this)==null && utils.isOutOfBounds(this,screen.width,screen.height)==false) {
                break;
            }

        }

    }

     calculateTextWidth(txt) {
        let ctx = screen.utilCanvas.getContext('2d');
        ctx.font = this.fontSize+'px Arial'; // 设置和 p5.js 相同的字体
        return ctx.measureText(txt).width;
    }
    draw(p) {
        super.draw(p);
        this.color=config.levelMap[this.level];
        this.fontSize=setting.value.fontSize;
        this.height=setting.value.fontSize+10;
        this.width=p.textWidth(this.context)+20;
        this.ctx.strokeStyle="blue";
        p.fill(this.color)
        if (Date.now()<this.showAfterCreateMaxTime){
            p.stroke(Math.random()*255,Math.random()*255,Math.random()*255,128);
            p.fill("rgba(250,5,5,0.71)")
        }
        p.strokeWeight(2);
        p.rectMode(p.CORNER)
        p.rect(this.x,this.y,this.width,this.height,5)

        p.fill(255);

        p.strokeWeight(2);
        p.textAlign(p.CENTER,p.CENTER);
        if(this.isSelected){
            p.strokeWeight(10);
            p.rectMode(p.CORNER)
            p.stroke("rgb(250,239,3)")
            p.line(this.x-5,this.y+(this.height/2-5),this.x+this.width+5,this.y+(this.height/2-5))
        }
        p.stroke(1)
        p.strokeWeight(1)
        p.textSize(this.fontSize)

        p.fill("rgb(3,0,0)")
        p.text(this.context, this.x,this.y,this.width,this.height);


    }

    onClick() {
        super.onClick();
        let word={
            content:this.context,
            des:this.des,
            level:this.level
        }
        // ElMessage({
        //     type:"success",
        //     message:this.des
        // })
        new des(this.x-(this.width/2),this.y+this.height,this.context,this.des);
        screen.playerAudio(this.context);
        let clickDelay = Date.now()-this.lastClick;
        if (clickDelay<this.doubleClickMaxDelay){
            this.lastClick=0;
            screen.wordDoubleClick(word);
            return;
        }
        this.lastClick=Date.now();
        screen.wordOnClick(word);
    }


}